﻿@model Bricks.Business.Models.Account.RoleInfo

@{
    ViewBag.Title = "权限组管理";
    Layout = "~/Views/Shared/_UserPage.cshtml";
}

@*实现对EasyUI的DataGird控件操作的JS代码*@
<script type="text/javascript">
    $(function () {
        //当页面首次刷新的时候执行的事件
        initTable();

        initActionPathTable();  //权限路径列表
    });
    //实现对DataGird控件的绑定操作

    function initTable(queryData) {
        $('#myTreeGrid').treegrid({   //定位到Table标签，Table标签的ID是myTreeGrid
            url: '/RoleInfo/GetRoleDataList',   //指向后台的Action来获取当前用户的信息的Json格式的数据
            //title: '菜单管理',  //标识
            //width: 'auto',
            //height: 500,
            nowrap: true,
            autoRowHeight: false,
            striped: true,
            collapsible: true,
            rownumbers: true,
            //sortName: 'ID',    //根据某个字段给easyUI排序
            //sortOrder: 'asc',
            remoteSort: false,
            idField: 'ID',
            treeField: 'RoleName',
            parentField: 'PID',
            checkOnSelect: true,
            selectOnCheck: true,
            singleSelect: true,
            queryParams: queryData,  //异步查询的参数
            toolbar: '#myToolBar',
            columns: [[
                { title: '@Html.LabelFor(model => model.RoleName)', field: 'RoleName', width: 200, sortable: true },
                { title: '@Html.LabelFor(model => model.RoleCode)', field: 'RoleCode', width: 135, sortable: true },
                { title: '@Html.LabelFor(model => model.Description)', field: 'Description', width: 230, sortable: true }
            ]],
            //点击事件的处理：构造权限路径
            onClickRow: function (index, row) {
                var selectedRow = $("#myTreeGrid").treegrid("getSelected");
                if (selectedRow) {
                    $('#purviewPathDataGrid').datagrid('load', { roleID: selectedRow.ID });
                }
                //$.EasyuiSlide("点击事件测试");
            }
        });
    }

    //初始化权限路径列表
    function initActionPathTable(queryData) {
        $('#purviewPathDataGrid').datagrid({   //定位到Table标签，Table标签的ID是myDataGrid
            //   MenuInfo是控制器，GetData是控制器中获取用户数据的一个Action
            url: '/Purview/GetRoleMenuActionPathList',   //指向后台的Action来获取当前用户的信息的Json格式的数据
            //title: '菜单管理',  //标识
            //width: 'auto',
            //height: 500,
            nowrap: true,
            autoRowHeight: false,
            striped: true,
            collapsible: true,
            rownumbers: true,
            //sortName: 'ID',    //根据某个字段给easyUI排序
            //sortOrder: 'asc',
            remoteSort: false,
            idField: 'ID',
            checkOnSelect: true,
            selectOnCheck: true,
            singleSelect: true,
            queryParams: queryData,  //异步查询的参数
            toolbar: '#purviewPathToolBar',
            columns: [[
                { title: '权限路径', field: 'PurviewPath', width: 500, sortable: true }
            ]]
        });
    }

    var RoleInfo = {
        //新增
        Add: function () {
            var row = $("#myTreeGrid").treegrid("getSelected");
            if (row) {
                RoleInfo.Mutual("新增", "/RoleInfo/Edit?PID=" + row.ID);
            }
            else { RoleInfo.Mutual("新增", "/RoleInfo/Edit"); }
        },

        //修改
        Modify: function () {
            var row = $("#myTreeGrid").treegrid("getSelected");
            if (row) {
                RoleInfo.Mutual("修改", "/RoleInfo/Edit?ID=" + row.ID);
            }
            else { $.EasyuiSlide("请选择一条记录"); }
        },

        //实体操作
        Mutual: function (title, href) {
            $.ModelDialog({
                title: title, href: href, width: 320,
                handler: function () {
                    $.ModelForm({
                        validate: true, button: $(this),
                        id: "#form-model",
                        url: "/RoleInfo/Edit",
                        success: function (result) { RoleInfo.Reload(); }
                    });
                },
                onLoad: function () { RoleInfo.FormInit(); }
            });
        },

        //弹窗初始化
        FormInit: function () {
            //定义父级的机构树
            $("#PID").combotree({
                width: 155, url: "/RoleInfo/GetRoleComboTree"
            });
        },

        //删除
        Delete: function () {
            var arr = $('#myTreeGrid').datagrid('getSelections');

            if (arr.length == 1) {
                $.messager.confirm('操作提示', "确认删除[ " + arr[0].RoleName + " ]吗？", function (r) {
                    if (r) {
                        $.post("/RoleInfo/Delete?ID=" + arr[0].ID, { query: arr.join(",") }, function (result) {
                            if (result.ResultType == 0) {  //0表示成功
                                $.EasyuiSlide(result.Message);
                                RoleInfo.Reload();
                            }
                            else {
                                $.EasyuiMsg(result.Message);
                            }
                        });
                    }
                });

            } else {
                $.EasyuiMsg("请选择一条数据");
            }
            return false;
        },

        //向上
        Up: function () {
            RoleInfo.Order("Up");
        },

        //向下
        Down: function () {
            RoleInfo.Order("Down");
        },

        //向上或向下方法
        Order: function (order) {
            var row = $("#myTreeGrid").treegrid("getSelected");
            if (row) {
                //id：识别当前记录；pid：识别涉及调整的记录集合；order：向上或向下
                $.post("/RoleInfo/SetOrder", { id: row.ID, pid: row.PID, order: order }, function (result) {
                    if (result.ResultType == 0) {
                        $.EasyuiSlide(result.Message);  //成功则右下角提示
                        RoleInfo.Reload();
                    }
                    else {
                        $.EasyuiMsg(result.Message);  //失败则弹出框提示
                    }
                });
            }
            else { $.EasyuiSlide(nullrowmsg); }
        },

        //权限组授权
        Purview: function () {
            var arr = $('#myTreeGrid').datagrid('getSelections');
            if (arr.length == 1) {
                RoleInfo.RolePurviewMutual("权限组授权", "/Purview/RolePurview?roleID=" + arr[0].ID, arr[0].ID);
            } else {
                //$.messager.alert('操作提示', '请选择一条数据!', 'warning');
                $.EasyuiMsg("请选择一条数据");
            }
        },

        //机构授权操作
        RolePurviewMutual: function (title, href, id) {
            $.ModelDialog({
                title: title, href: href, width: 650, maximizable: true,
                handler: function () {
                    $.ModelForm({
                        validate: true, button: $(this),
                        id: "#form-rolePurview",
                        url: "/Purview/RolePurviewPost",
                        success: function (result) {
                            //RoleInfo.Reload();
                            $('#purviewPathDataGrid').datagrid('load', { roleID: id });
                        }
                    });
                },
                onLoad: function () {
                    //RoleInfo.FormInit();
                }
            });
        },

        //刷新
        Reload: function () {
            $("#myTreeGrid").treegrid("reload");
        },

        //刷新权限路径
        ReloadPurviewPath: function () {
            $("#purviewPathDataGrid").datagrid("reload");
        },

        //权限组单独授权
        AlonePurview: function () {
            $.EasyuiMsg("权限组单独授权-后续考虑，因为在权限组授权中其实已经包含此逻辑");
        }
    }
</script>

<div class="easyui-layout" data-options="fit:true">
    <!--左边为权限组管理列表-->
    <div region="west" data-options="border:false" style="width: 600px;">
        <div class="easyui-panel" title="权限组管理" fit='true' iconcls='icon-special'>
            <table id="myTreeGrid" fit="true"></table>
            @*<div id="myToolBar">
                <a href='javascript:void(0)' id='btnAdd' class='easyui-linkbutton' onclick='RoleInfo.Add()' data-options='plain:true,iconCls:"icon-add"'>新增</a>
                <a href='javascript:void(0)' id='btnModify' class='easyui-linkbutton' onclick='RoleInfo.Modify()' data-options='plain:true,iconCls:"icon-big_pencil"'>修改</a>
                <a href='javascript:void(0)' id='btnDelete' class='easyui-linkbutton' onclick='RoleInfo.Delete()' data-options='plain:true,iconCls:"icon-cancel"'>删除</a>
                <a href='javascript:void(0)' id='btnUp' class='easyui-linkbutton' onclick='RoleInfo.Up()' data-options='plain:true,iconCls:"icon-up"'>向上</a>
                <a href='javascript:void(0)' id='btnDown' class='easyui-linkbutton' onclick='RoleInfo.Down()' data-options='plain:true,iconCls:"icon-down"'>向下</a>
                <a href='javascript:void(0)' id='btnAssign' class='easyui-linkbutton' onclick='RoleInfo.Purview()' data-options='plain:true,iconCls:"icon-preferences"'>权限组授权</a>
                <a href='javascript:void(0)' id='btnReload' class='easyui-linkbutton' onclick='RoleInfo.Reload()' data-options='plain:true,iconCls:"icon-reload"'>刷新</a>
            </div>*@
            <div id="myToolBar">@Html.Raw(ViewBag.ToolBarInfo)</div>
        </div>
    </div>
    <!--右边为权限路径列表，根据选择的权限组进行切换-->
    <div region="center" style="border: 0px; height: 500px; width: 100%;">
        <div class="easyui-panel" title="权限路径预览" fit='true' iconcls='icon-special'>
            <table id="purviewPathDataGrid" fit="true"></table>
            <div id="purviewPathToolBar">
                <a href='javascript:void(0)' id='btnAlonePurview' class='easyui-linkbutton' onclick='RoleInfo.AlonePurview()' data-options='plain:true,iconCls:"icon-preferences"'>单独授权</a>
                <a href='javascript:void(0)' id='btnReloadPurviewPath' class='easyui-linkbutton' onclick='RoleInfo.ReloadPurviewPath()' data-options='plain:true,iconCls:"icon-reload"'>刷新</a>
            </div>
        </div>
    </div>
</div>
